package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.FollowDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.user.service.ApUserService;
import com.heima.user.service.UserService;
import com.heima.utils.thread.ApUserThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @Author: JW
 * @Description: TODO
 * @DateTime: 2022-02-20 10:17
 **/
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private ApUserFollowService apUserFollowService;

    @Autowired
    private ApUserService apUserService;

    @Autowired
    private ApUserFanService apUserFanService;

    /**
     * @param followDto
     * @Author: JW
     * @Description: TODO  关注
     * @Date: 11:24 2022-02-20
     * @Param: [followDto]
     * @return: com.heima.model.common.dtos.ResponseResult
     */
    @Override
    @Transactional
    public ResponseResult follow(FollowDto followDto) {

        //参数校验
        if (ObjectUtils.isEmpty(followDto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        ApUser user = ApUserThreadUtil.getUser();

        if (user == null){
            throw new RuntimeException("非法操作");
        }

        //此时状态为0时 为关注
        if (followDto.getOperation() == 0){

            //保存关注表
            //补全数据
            ApUserFollow apUserFollow = new ApUserFollow();


            apUserFollow.setUserId(user.getId());
            apUserFollow.setFollowId(followDto.getAuthorId());

            //查询出作者
            ApUser apUser = apUserService.getById(followDto.getAuthorId());

            apUserFollow.setFollowName(apUser.getName());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setLevel(0);
            apUserFollow.setNotice(1);

            apUserFollowService.save(apUserFollow);

            //保存粉丝表
            //补全数据
            ApUserFan apUserFan = new ApUserFan();


            //查询当前用户
            ApUser currentApUser = apUserService.getById(user.getId());


            apUserFan.setUserId(followDto.getAuthorId());
            apUserFan.setFansId(ApUserThreadUtil.getUser().getId());
            apUserFan.setFansName(currentApUser.getName());
            apUserFan.setLevel(0);
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay(1);
            apUserFan.setIsShieldLetter(0);
            apUserFan.setIsShieldComment(0);

            apUserFanService.save(apUserFan);
        }else if(followDto.getOperation() == 1){

            //删除关注表
            apUserFollowService.remove(Wrappers.lambdaQuery(ApUserFollow.class)
                    .eq(ApUserFollow::getUserId,user.getId())
                    .eq(ApUserFollow::getFollowId,followDto.getAuthorId()));

            //删除粉丝表
            apUserFanService.remove(Wrappers.lambdaQuery(ApUserFan.class)
                    .eq(ApUserFan::getUserId,followDto.getAuthorId())
                    .eq(ApUserFan::getFansId,user.getId()));

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ApUserFollow getFollowInfo(Integer userId, Integer authorId) {
        if(userId==null&&authorId==null){
            return null;
        }

        ApUserFollow one = apUserFollowService.getOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId, userId).eq(ApUserFollow::getFollowId, authorId));
        return one;

    }
}
